<html lang="en">
<head>
    <title>区域管理</title>
    <#include "../common/header.ftl">
    <link rel="stylesheet" href="/js/plugins/treeview/bootstrap-treeview.min.css" type="text/css" />
    <script type="text/javascript" src="/js/plugins/treeview/bootstrap-treeview.min.js"></script>
    <script type="text/javascript">
        $(function () {

            //区域数据
            var regionData;

            if(!regionData){
                $.post("/region/queryByParentId", function (data) {
                    regionData = data;
                })
            }
            $("#parentName").click(function () {
                //$('#treeview').treeview('remove');
                $('#treeview').treeview({
                    color: "#428bca",
                    showTags: true,
                    data: regionData,
                    levels:5,
                    //点击+号图标触发方法
                    lazyLoad:function(node){
                        $.get("/region/queryByParentId",{pid:node.id}, function (data) {
                            $('#treeview').treeview('addNode', [ data, node ]);
                        })
                    },
                    //选择节点触发时间
                    onNodeSelected:function (event, node) {
                        $("#parentName").val(node.text);
                        $("#parentId").val(node.id);
                        $("#treeview").hide();//选中树节点后隐藏树
                    }
                });
                $('#treeview').show()
            })
            //编辑/添加
            $(".inputBtn").click(function () {
                //弹出模态框
                $("#editModal").modal("show");

                $("#treeview").html("");
                //数据复原
                $("#editForm").clearForm(true);

                //区域回显数据
                var data = $(this).data("json");
                if(data){
                    $(".modal-title").html("区域编辑");
                    $("input[name='id']").val(data.id);
                    $("input[name='name']").val(data.name);
                    $("input[name='sn']").val(data.sn);
                    $("input[name='parent.id']").val(data.parentId);
                    $("input[name='parent.name']").val(data.parentName || "顶级");

                }
            })
            
            $(".submitBtn").click(function () {
                //模态框表单提交
                $("#editForm").ajaxSubmit(function (data) {
                    if(data.success){
                        window.location.reload();
                    }else{
                        $.messager.alert("温馨提示", data.msg)
                    }
                })
            })

            //设置热门
            $(".hotBtn").click(function () {
                var hot = $(this).data("hot");
                var id = $(this).data("id");
                $.get("/region/changeHotValue", {hot:hot, id:id}, function (data) {
                    if(data.success){
                        window.location.reload();
                    }else{
                        $.messager.alert("温馨提示", data.msg);
                    }
                })
            })

            //关联地区
            $(".destShowBtn").click(function () {

                $("#refplace").html('');

                var id = $(this).data("id");
                $.get("/region/getRefDestination", {rid:id}, function (data) {
                    var html = ''
                    $.each(data, function (index, value) {
                        html +='<a href="javascript:;" class="btn btn-link">' + value.name + '</a>'
                    })
                    $("#refplace").html(html);
                    $("#destModal").modal("show");
                })
            })
        })
    </script>
</head>
<body>
<!--左侧菜单回显变量设置-->
<#assign currentMenu="region">

<div class="container-fluid " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-2">
            <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-10">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">区域管理</h1>
                </div>
            </div>
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="/region/list" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <a href="JavaScript:;" class="btn btn-success inputBtn"><span class="glyphicon glyphicon-plus"></span>  添加</a>
            </form>

            <table class="table table-striped table-hover" >
                <thead>
                    <tr>
                        <th>序号</th>
                        <th>名称</th>
                        <th>编号</th>
                        <th>区域类型</th>
                        <th>关联地区</th>
                        <th>排序</th>
                        <th>操作</th>
                    </tr>
                </thead>
               <#list pageInfo.list as entity>
                   <tr>
                       <td>${entity_index+1}</td>
                       <td>${entity.name}</td>
                       <td>${entity.sn!}</td>
                       <td>${entity.typeDisplay!}</td>
                       <td><a href="JavaScript:;" class="destShowBtn" data-id="${entity.id}">查看</a></td>
                       <td>${entity.sequence!}</td>
                       <td>
                           <#if  entity.ishot>
                            <a class="btn btn-success btn-xs hotBtn" href="javascript:;" data-hot='0' data-id="${entity.id}">
                                <span class="glyphicon glyphicon-minus-sign"></span> 取消热门
                            </a>

                           <#else>
                            <a class="btn btn-default btn-xs hotBtn" href="JavaScript:;" data-hot="1" data-id="${entity.id}">
                                <span class="glyphicon glyphicon-tag"></span>设为热门
                            </a>
                           </#if>

                           <a class="btn btn-info btn-xs inputBtn" href="javascript:;" data-json='${entity.jsonString}'>
                               <span class="glyphicon glyphicon-pencil"></span> 编辑
                           </a>
                           <a href="javascript:;" class="btn btn-danger btn-xs deleteBtn"
                              data-url="/region/delete?id=${entity.id}">
                               <span class="glyphicon glyphicon-trash"></span> 删除
                           </a>
                       </td>
                   </tr>
               </#list>
            </table>
            <#--分页-->
            <#include "../common/page.ftl"/>
        </div>
    </div>
</div>

<#--区域编辑模态框-->
<div class="modal fade" id="editModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span> </button>
                <h4 class="modal-title">区域添加/编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/region/saveOrUpdate" method="post" id="editForm">
                    <input type="hidden" value="" name="id">
                    <div class="form-group" >
                        <label  class="col-sm-3 control-label">上级区域：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="parentName" value="" id="parentName" >
                            <input type="hidden" class="form-control" name="parent.id"  value="" id="parentId" >
                            <div id="treeview" style="display: none; position:absolute; z-index:1010; background-color:white;  width: 250px"></div>
                        </div>
                    </div>

                    <div class="form-group" >
                        <label  class="col-sm-3 control-label">区域名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="name" value="" placeholder="请输入区域名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-3 control-label">区域编号：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control"  name="sn" value="" placeholder="请输入区域编号">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary submitBtn" >保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal" >取消</button>
            </div>
        </div>
    </div>
</div>


<#--关联地区查看-->
<div class="modal fade" id="destModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span> </button>
                <h4 class="modal-title">关联地区</h4>
            </div>
            <div class="modal-body">
                    <div class="form-group">
                        <div class="col-sm-12" id="refplace" style="width: 100%">
                        </div>
                    </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal" >确定</button>
            </div>
        </div>
    </div>
</div>

</body>
</html>